package com.ruoyi.material.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material.domain.WarehouseInfo;
import com.ruoyi.material.service.IWarehouseInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存管理Controller
 *
 * @author ruoyi
 * @date 2022-08-26
 */
@RestController
@RequestMapping("/material/WarehouseInfo")
public class WarehouseInfoController extends BaseController {
    @Autowired
    private IWarehouseInfoService warehouseInfoService;

    /**
     * 查询库存列表
     */
    @PreAuthorize("@ss.hasPermi('material:WarehouseInfo:list')")
    @GetMapping("/list")
    public AjaxResult list(WarehouseInfo warehouseInfo) {
        startPage();
        List<WarehouseInfo> list = warehouseInfoService.selectWarehouseInfoList(warehouseInfo);
        long sum = list.stream().mapToLong(WarehouseInfo::getRemainderCount).sum();
        TableDataInfo dataTable = getDataTable(list);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("dataTable",dataTable);
        ajaxResult.put("materialTotal",sum);
        return ajaxResult;
    }

    /**
     * 获取库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:WarehouseInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(warehouseInfoService.selectWarehouseInfoById(id));
    }

    /**
     * 修改库存
     */
    @PreAuthorize("@ss.hasPermi('material:WarehouseInfo:edit')")
    @Log(title = "库存管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarehouseInfo warehouseInfo) {
        return toAjax(warehouseInfoService.updateWarehouseInfo(warehouseInfo));
    }


    //分配未绑定数量distribution
    @PreAuthorize("@ss.hasPermi('material:WarehouseInfo:distribution')")
    @Log(title = "库存管理", businessType = BusinessType.UPDATE)
    @PutMapping("/distribution")
    public AjaxResult distribution(@RequestBody WarehouseInfo warehouseInfo) {
        return warehouseInfoService.distribution(warehouseInfo);
    }
    //解绑已绑定数量
    @PreAuthorize("@ss.hasPermi('material:WarehouseInfo:unbound')")
    @Log(title = "库存管理", businessType = BusinessType.UPDATE)
    @PutMapping("/unbound")
    public AjaxResult unbound(@RequestBody WarehouseInfo warehouseInfo) {
        return toAjax(warehouseInfoService.updateWarehouseInfo(warehouseInfo));
    }

}
